package com.dz.wx.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;

import com.google.common.collect.ImmutableMap;

@Slf4j
public abstract class BaseController {

	private final Properties errorCodeMap = new Properties();

	{
		try {
			errorCodeMap.load(BaseController.class.getResourceAsStream("/error_code.properties"));
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * 接口数据返回
	 * @param errorCode
	 * @param data
	 * @return
	 */
	protected Map<String, Object> rtnParam(Integer errorCode, Object data) {
		// 正常的业务逻辑
		if (errorCode == 0) {
			return ImmutableMap.of("errorCode", errorCode, "data", (data == null) ? new Object() : data);
		} else {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("errorCode", errorCode);
			dataMap.put("msg", errorCodeMap.getProperty(String.valueOf(errorCode)));
			return dataMap;
		}
	}
}
